<?
	/* Jaiku - stream reader plugin */	

	// return type of service
	function jaiku_getType() { return "status"; }

	// extra the service- action  line ("ike: "I tweeted on jaiku.com")
	function jaiku_getAction($line) {
		return "<a href='?service=jaiku'><img class='icon' src='".serviceIcon("jaiku")."'/></a> I posted on <a href='http://jaiku.com'>jaiku.com</a>";
	}

	// extract and return the correct caption
	function jaiku_getCaption($line) {
		$entry=explode("|",$line);
		return $entry[3];
	}

	// extract and return the correct post
	function jaiku_getPost($line) {
		return "";
	}

	// extract and return the correct link to the item
	function jaiku_getLink($line) {
		$entry=explode("|",$line);
		return $entry[5];
	}	

	// display information about us with a link to the profile
	//  -- this is called to display list of services that are linked (admin-functions.php)
	function jaiku_identify($item) {
		$entry=explode("|",$item);
		return "Jaiku account <a href='".$entry[3]."'>".$entry[2]."</a>";
	}	


	// get service + profile information
	function jaiku_addProfile() {
	
			$serviceID=$_POST['addservice'];
			$username=$_POST['username'];
	
			// requires parsing the userpage for the rss feed url
			$rsslink="http://".$username.".jaiku.com/feed/rss";
			
			writeService($serviceID,$username,$rsslink);
	}

	// download latest items from this service
	function jaiku_download($serviceid,$filename)  {
		$reader = new XMLReader();
		
		if (!$reader->open($filename )) {
			print "error reading file -<br/>".$filename;
		}

			
		set_time_limit(30);


		$count=0;
		
		$itemids=array();
		$continueParsing=true;

		while ($reader->read() && $continueParsing) {
				
				if ($reader->nodeType==XMLREADER::ELEMENT) {

					if ($reader->localName=="item") {
						$guid=null;
						$title=null;
						$mediaurl=null;
						$thumb=null;
						$link=null;
						$pubdate=null;
						$content=null;
						
						while ($reader->read()) {

							if ($reader->name=="guid" && !$guid) {
								$reader->read();
								$guid=$reader->value;
							}
							
							if ($reader->localName=="pubDate" && !$pubdate) {
								$reader->read();
								$pubdate=$reader->value;
							}

							if ($reader->name=="title" && !$title) {
								$reader->read();
								$title=$reader->value;
							}
		

							if ($reader->name=="link" && !$link) {
								$reader->read();
								$link=$reader->getAttribute("href");
							}
							
							if ($reader->localName=="item") {
								if ($guid) {
								
									$indx=date("YmdHis",strtotime($pubdate));
									
									// see if this is an @ reply, and then skip it
									$username=substr($title,0,strpos($title,":"));
									$title=substr($title,strpos($title,":")+2);
									
									if (substr($title,0,1)!="@" || $includeATtweets) {
										if (newGuid($guid)) {
											$item[$indx]->title=$title;
											$item[$indx]->post=$title;
											$item[$indx]->link=$link;
											$item[$indx]->pubDate=$pubdate;
											$item[$indx]->guid=$guid;
											$item[$indx]->extra="|".$link."|".$username;
										} else $continueParsing=false;
									} 
									
								}
								break;
							}
						}
						
						//print $item[$guid]["title"]." : <img src='".$item[$guid]["media"]."'>";
					}
					set_time_limit(30);
				}
				
		}  
		
		insertEntries($item,"jaiku",$serviceid);
	}
?>